<?

require_once('DataObject/DomainModel.php');

class Pages extends DomainModel {
	protected $id;	//primaryKey=yes, dataType=int, maxLength=, allowNulls=no
	protected $statusesId;	//primaryKey=no, dataType=smallint, maxLength=, allowNulls=yes
	protected $title;	//primaryKey=no, dataType=varchar, maxLength=250, allowNulls=no
	protected $teaser;	//primaryKey=no, dataType=varchar, maxLength=500, allowNulls=yes
	protected $content;	//primaryKey=no, dataType=text, maxLength=65535, allowNulls=yes
	protected $metaTitle;	//primaryKey=no, dataType=varchar, maxLength=150, allowNulls=yes
	protected $metaDescription;	//primaryKey=no, dataType=varchar, maxLength=500, allowNulls=yes
	protected $metaTags;	//primaryKey=no, dataType=varchar, maxLength=500, allowNulls=yes
	protected $addDate;	//primaryKey=no, dataType=timestamp, maxLength=, allowNulls=no
	protected $lastUpdateDate;	//primaryKey=no, dataType=timestamp, maxLength=, allowNulls=no
	protected $usersId;	//primaryKey=no, dataType=int, maxLength=, allowNulls=no
	protected $toplineText;
	protected $staticUrl;
	
	protected $status;
	
	public function getStatus(){
		if (is_null($this->status)){
			$this->status = ModelMapper::instance('Statuses')->find($this->statusesId);
		}
		
		return $this->status;
	}
}




/**
 * Class for DB_Object ModelMapper
 *
 * @category   RFC_Model
 * @package    RFC_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
*/
class PagesMapper extends ModelMapper {
	function __construct(){
		parent::__construct();
		$this->tableName = 'pages';
		$this->selectViewName = 'v_pages';
		$this->selectFields = '
				id,
				statuses_id,
				title,
				teaser,
				content,
				meta_title,
				meta_description,
				meta_tags,
				add_date,
				last_update_date,
				users_id,
				topline_text,
				static_url,
				st_id,
				st_statuses_catalogues_id,
				st_status,
				st_description,
				sc_id,
				sc_catalogue
		';

		$this->findQuery = self::$DB->prepare("
			SELECT ".$this->selectFields."
			FROM ".$this->selectViewName."
			WHERE id = ?
		");
		$this->insertQuery = self::$DB->prepare("
			CALL p_add_pages (@p_id,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?
		)");


		$this->updateQuery = self::$DB->prepare("
			CALL p_upd_pages (
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?,
				?

		)");

		$this->deleteQuery = self::$DB->prepare("
			CALL p_del_pages (?)
		");

		$this->cacheTime = null;
	}

	protected function doLoad($array){
		$acuire = str_replace('Mapper', '', get_class($this));
		$obj = new $acuire($array['id']);
		$obj->setId($array['id']);
		$obj->setStatusesId($array['statuses_id']);
		$obj->setTitle($array['title']);
		$obj->setTeaser($array['teaser']);
		$obj->setContent($array['content']);
		$obj->setMetaTitle($array['meta_title']);
		$obj->setMetaDescription($array['meta_description']);
		$obj->setMetaTags($array['meta_tags']);
		$obj->setAddDate($array['add_date']);
		$obj->setLastUpdateDate($array['last_update_date']);
		$obj->setUsersId($array['users_id']);
		$obj->setToplineText($array['topline_text']);
		$obj->setStaticUrl($array['static_url']);

		$arr = ArrayUtils::splitByPrefix($array, array('st_', 'sc_'));
		
		if (!empty($arr['st_']))
			ModelMapper::instance('Statuses')->loadArray($arr['st_']);
		if (!empty($arr['sc_']))
			ModelMapper::instance('StatusesCatalogues')->loadArray($arr['sc_']);
		
		return $obj;
	}

	function insert(DomainModel $obj){

		$values = array(
			$obj->getStatusesId(),
			$obj->getTitle(),
			$obj->getTeaser(),
			$obj->getContent(),
			$obj->getMetaTitle(),
			$obj->getMetaDescription(),
			$obj->getMetaTags(),
			$obj->getAddDate(),
			$obj->getLastUpdateDate(),
			$obj->getUsersId(),
			$obj->getToplineText(),
			$obj->getStaticUrl()
		);

		$this->doStatement($this->insertQuery, $values);
		$obj->setId(self::$DB->getOne("SELECT @p_id"));
	}

	function update(DomainModel $obj){
		$values = array(
			$obj->getId(),
			$obj->getStatusesId(),
			$obj->getTitle(),
			$obj->getTeaser(),
			$obj->getContent(),
			$obj->getMetaTitle(),
			$obj->getMetaDescription(),
			$obj->getMetaTags(),
			$obj->getAddDate(),
			$obj->getLastUpdateDate(),
			$obj->getUsersId(),
			$obj->getToplineText(),
			$obj->getStaticUrl()
		);

		$this->doStatement($this->updateQuery, $values);
	}

	function delete(DomainModel $obj){
		$this->doStatement($this->deleteQuery, array($obj->getId()));
	}

	function newId(){

		$row = self::$DB->getRow('SHOW TABLE STATUS LIKE pages');
		return $row['Auto_increment'];

	}
}

class PagesCollection extends ModelCollection {
	function add(Pages $model){
		$this->doAdd($model);
	}
}

class PagesCollectionExtended extends PagesCollection {
	private $stmt;
	private $valueArray;
	private $mapper;
	private $run = false;

	function __construct(ModelMapper $mapper, $stmtHandle, $valueArray){
		parent::__construct();
		$this->stmt = $stmtHandle;
		$this->valueArray = $valueArray;
		$this->mapper = $mapper;
	}

	function notifyAccess(){
		if (!$this->run){
			$result = $this->mapper->doStatement($this->stmt, $this->valueArray,$this->mapper->getCacheTime());
			$this->init_db($result, $this->mapper);
			$this->run = true;
		}
	}
}

?>